package com.sth.edu.controller;


import com.sth.commonutils.R;
import com.sth.edu.entity.Chapter;
import com.sth.edu.entity.chapter.ChapterVo;
import com.sth.edu.service.ChapterService;
import com.sth.edu.utils.UUIDUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author sth
 * @since 2020-12-05
 */
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
@Api(description = "课程大纲")
public class ChapterController {

    @Autowired
    private ChapterService chapterService;

    //课程大纲列表,根据课程id进行查询
    @GetMapping("getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId) {
        List<ChapterVo> list = chapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("allChapterVideo", list);
    }
    // 添加小节
    @PostMapping("addChapter")
    public R addChapter(@RequestBody Chapter chapter) {
        chapter.setId(UUIDUtil.get4UUID());
        chapterService.save(chapter);
        return R.ok();
    }

    // 根据章节id查询
    @GetMapping("getChapterInfo/{chapterId}")
    public R getChapter(@PathVariable String chapterId) {
        Chapter chapter = chapterService.getById(chapterId);
        return R.ok().data("chapter", chapter);
    }

    // 修改章节
    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody Chapter chapter) {
        chapterService.updateById(chapter);
        return R.ok();
    }

    // 删除
    @DeleteMapping("{chapterId}")
    public R deleteChapter(@PathVariable String chapterId) {
        Boolean flag = chapterService.deleteChapter(chapterId);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }

    }
}

